﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLVMBAYDatacess;
using QLVMBAYDomain;
namespace QLVMBAY.Pages
{
    public partial class pGioHang : System.Web.UI.Page
    {
        class TTDatCho
        {
            public string MaVe{get;set;}
            public string LoaiVe{get;set;}
            public int SoLuong{get;set;}
            public decimal TongTien{get;set;}
            public TRANG_THAI TrangThai { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadTT();
 
            }

        }

        private void LoadTT()
        {
            var ngDung = (NguoiDung)Session[ClientComm.KEY_DANG_NHAP];
            if (ngDung.LoaiND == LOAI_NGUOI_DUNG.USER)
            {
                var getCT = DatchoDA.Inst.GetListDCByHK(ngDung.MaHanhKhach);
                var lst = getCT.GroupBy(r => r.MaVe).Select(r => new TTDatCho()
                {
                    MaVe = r.Key,
                    LoaiVe = getCT.Where(t => t.MaVe == r.Key).FirstOrDefault().LoaiDatCho == LOAI_DAT_CHO.KHU_HOI ? "Khứ hồi" : "Một chiều",
                    SoLuong = r.Count(),
                    TongTien = r.Sum(t => t.TongTien),
                    TrangThai = getCT.Where(t => t.MaVe == r.Key).FirstOrDefault().TrangThai
                });
                grdDS.DataSource = lst;
                grdDS.DataBind();
                pnHuy.Visible = false;
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }

        protected void grdDS_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var row = grdDS.Rows[int.Parse(e.CommandArgument.ToString())];
            var lstDC = DatchoDA.Inst.GetDatChoByMaVe(row.Cells[0].Text);
            var NgayBay = lstDC.Where(r => r.LuotDatCho == LUOT_DAT_CHO.CHIEU_DI).Select(r => r.ItemChiTietCBay.ItemChuyenBay.NgayBay).FirstOrDefault();
            if (NgayBay > DateTime.Now)
            {
                pnHuy.Visible = true;
                lblMaVe.Text = row.Cells[0].Text;
                var huy = DatchoDA.Inst.GetPhiHuyByMaVe(lblMaVe.Text);
                lblHuy.Text = huy.ToString();
                lblPhiHuy.Text = huy.ToString("#,##0") + " VNĐ";
            }
            else
            {
                Page.RegisterStartupScript("Err",
                                                                                "<script language='javascript'>alert('Mã vé không được phép hủy.');</script>");
                return;
            }
            
        }

        protected void grdDS_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItemIndex != -1)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    var item=(TTDatCho)e.Row.DataItem;
                    var lblTongTien = e.Row.FindControl("lblTongTien") as Label;
                    lblTongTien.Text = item.TongTien.ToString("#,##0") + " VNĐ";
                    
                    var lblTrangThai = e.Row.FindControl("lblTrangThai") as Label;
                    lblTrangThai.Text = item.TrangThai == TRANG_THAI.DAT_CHO ? "Đặt chổ" : "Hủy";

                }
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            var Res = DatchoDA.Inst.HuyVeByMaVe(lblMaVe.Text, decimal.Parse(lblHuy.Text));
            if (Res)
            {
                Page.RegisterStartupScript("Err",

                    "<script language='javascript'>alert('Hủy vé được thực hiện thành công.');</script>");
                LoadTT();
                return;
            }
            else
            {
                Page.RegisterStartupScript("Err",

                                   "<script language='javascript'>alert('Có lỗi xãy ra việc hủy vé chưa được thực hiện.');</script>");
            }
        }
    }
}